var WSO = new ActiveXObject('WScript.Shell');
var FSO = new ActiveXObject('Scripting.FileSystemObject');
var DIR = new String(FSO.GetFile(WScript.ScriptFullName).ParentFolder);
var CWD = DIR.replace(/\\/g, '/');
var SNV = WSO.Environment('SYSTEM');
var UNV = WSO.Environment('USER');
var SQL = [
    '[client]',
    'port                    = 3366',
    '[mysql]',
    'default-character-set   = utf8',
    '[mysqld]',
    'port                    = 3366',
    'basedir                 = "' + CWD + '/"',
    'datadir                 = "' + CWD + '/data/"',
    'character-set-server    = utf8',
    'default-storage-engine  = INNODB',
    'sql-mode                = "STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"',
    'max_connections         = 318',
    'query_cache_size        = 8M',
    'table_cache             = 636',
    'tmp_table_size          = 16M',
    'thread_cache_size       = 15',
    'myisam_max_sort_file_size = 100G',
    'myisam_sort_buffer_size = 8M',
    'key_buffer_size         = 8M',
    'read_buffer_size        = 64K',
    'read_rnd_buffer_size    = 256K',
    'sort_buffer_size        = 208K',
    'innodb_additional_mem_pool_size = 2M',
    'innodb_flush_log_at_trx_commit  = 1',
    'innodb_log_buffer_size  = 1M',
    'innodb_buffer_pool_size = 8M',
    'innodb_log_file_size    = 10M',
    'innodb_thread_concurrency = 10'
];
var CL6 = [
    DIR + '\\bin\\mysqld.exe --install MySQL52',
    'net start MySQL52'
];
var CL7 = [
    'net stop MySQL52',
    DIR + '\\bin\\mysqld.exe --remove MySQL52'
];
var CNF = DIR + '\\my.ini';
if (FSO.FileExists(CNF)) {
    FSO.GetFile(CNF).Delete();
    try {
        for(var i = 0; i < CL7.length; ++i) WSO.Run(CL7[i], 0, true);
    } catch(e) {
        WSH.Echo(e.description);
    }
    var LOG = new Enumerator(FSO.GetFolder(DIR + '\\data\\').files);
    for(; !LOG.atEnd(); LOG.moveNext()) {
        LOG.item().Delete();
    }
    WSH.Echo('MySQL52 removed successfully.');
} else {
    var INI = FSO.OpenTextFile(CNF, 2, true);
    for(var i = 0; i < SQL.length; ++i) {
        INI.WriteLine(SQL[i]);
    }
    INI.Close();
    for(var i = 0; i < CL6.length; ++i) {
        WSO.Run(CL6[i], 0, true);
    }
    WSH.Echo('MySQL52 installed successfully.');
}
